let express = require('express');
let mysql = require('./db')


let router = express.Router();

module.exports = router;


router.get('/list', (req, res) => {
    let { page=1, limit=10 } = req.query;
    let index = (page - 1) * limit;
    mysql.query(`select * from books limit ${index},${limit};select count(*) as total from books;`, function (state, data) {
        if (state) {
            res.send({
                code: 200,
                msg: 'success',
                data
            })
        } else {
            res.send({
                code: 400,
                msg: 'error',
                data
            })
        }
    })
})

router.post('/details/:id', (req, res) => {
    let { id } = req.params;
    mysql.query(`select * from books where id=${id}`, (state, data) => {
        if (state) {
            res.send({
                code: 200,
                msg: 'success',
                data
            })
        } else {
            res.send({
                code: 400,
                msg: 'error',
                data
            })
        }
    })
})

router.post('/list/search/:book', (req, res) => {
    let { book } = req.params;
    mysql.query(`select * from books where book=${book}`, (state, data) => {
        if (state) {
            res.send({
                code: 200,
                msg: 'success',
                data
            })
        } else {
            res.send({
                code: 400,
                msg: 'error',
                data
            })
        }
    })
})

router.post('/list/:booktype', (req, res) => {
    let { booktype } = req.params;
    mysql.query(`select * from books where booktype=${booktype}`, (state, data) => {
        if (state) {
            res.send({
                code: 200,
                msg: 'success',
                data
            })
        } else {
            res.send({
                code: 400,
                msg: 'error',
                data
            })
        }
    })
})

router.post('/remove/:id', (req, res) => {
    let { id } = req.params;
    mysql.query(`DELETE FROM books where id=${id}`, (state, data) => {
        if (state) {
            res.send({
                code: 200,
                msg: '删除成功',
                data
            })
        } else {
            res.send({
                code: 400,
                msg: '删除失败',
                data
            })
        }
    })
})


router.put('/change/:id', (req, res) => {
    let {id}=req.params
    let {book,author,hot,booktype,brief} = req.body
    mysql.query(`update books set book='${book}'  where id='${id}';
                update books set author='${author}'  where id='${id}';
                update books set hot='${hot}'  where id='${id}';
                update books set booktype='${booktype}'  where id='${id}';
                update books set brief='${brief}'  where id='${id}';`, (state, data) => {
        if (state) {
            res.send({
                code: 200,
                msg: '修改成功',
                data
            })
        } else {
            res.send({
                code: 400,
                msg: '修改失败',
            })
        }
    })
})

router.post('/add', (req, res) => {
    let { book, author,booktype,hot,brief } = req.body;
    mysql.query(`insert into books values(NULL, '${book}', '${author}','${booktype}','${hot}',NULL,'${brief}',NULL);`, (state, data) => {
        if (state) {
            res.send({
                code: 200,
                msg: '注册成功',
                data
            })
        } else {
            res.send({
                code: 400,
                msg: '注册失败',
                data
            })
        }
    })
})

router.post('/remove', (req, res) => {
    let { data } = req.body;
    let {ids} = data
    let id=ids.join(',')
        mysql.query(`DELETE FROM books where id in(${id}) `, (state, data) => {
            if (state) {
                res.send({
                    code: 200,
                    msg: '删除成功',
                    data
                })
            } else {
                res.send({
                    code: 400,
                    msg: '删除失败',
                    data
                })
            }
        })
})

router.put('/changepic/:id', (req, res) => {
    let {id}=req.params
    let {pic} = req.body
    mysql.query(`update books set pic='${pic}'  where id='${id}'`, (state, data) => {
        if (state) {
            res.send({
                code: 200,
                msg: '修改成功',
                data
            })
        } else {
            res.send({
                code: 400,
                msg: '修改失败',
            })
        }
    })
})

router.post('/bookbox', (req, res) => {
    let { data } = req.body;
    let {ids} = data
    let ids2 =ids.split(',')
    let ids3 = ids2.map((item)=>{
        return item*1
    })
    let id=ids3.join(',')
        mysql.query(`select * from books where id in(${id}) `, (state, data) => {
            if (state) {
                res.send({
                    code: 200,
                    msg: '请求成功',
                    data
                })
            } else {
                res.send({
                    code: 400,
                    msg: '请求失败',
                    data
                })
            }
        })
})

router.put('/addbooklist/:id', (req, res) => {
    let {id}=req.params
    let {newbook} = req.body
    mysql.query(`update user set books='${newbook}'  where user_id='${id}'`, (state, data) => {
        if (state) {
            res.send({
                code: 200,
                msg: '修改成功',
                data
            })
        } else {
            res.send({
                code: 400,
                msg: '修改失败',
            })
        }
    })
})

module.exports = router
